<template>
  <a-form layout="horizontal" class="form" >
    <a-checkbox-group v-model="checkedList" @change="onchange">
      <a-row :gutter="8">
        <a-col :span="24"><a-checkbox :disabled="sceneDisabled" value="ALLSCENE">所有页面可见</a-checkbox></a-col>
        <a-col :span="24"><a-checkbox :disabled="functionDisabled" value="ALLFUNCTION">所有功能可操作</a-checkbox></a-col>
        <a-col :span="24"><a-checkbox :disabled="orgDisabled" value="ALLORG">所有组织权限</a-checkbox></a-col>
        <a-col :span="24"><a-checkbox :disabled="flowNodeDisabled" value="ALLFLOWNODE">所有流程节点权限</a-checkbox></a-col>
      </a-row>
    </a-checkbox-group>
  </a-form>
</template>
<script>
import { getUserSystemPermission, saveUserSystemPermission } from "@/services/system/api"

export default {
  data () {
    return {
      checkedList: [],
      sceneDisabled: true,
      functionDisabled: true,
      orgDisabled: true,
      flowNodeDisabled:true
    }
  },
  props: {
    userInfo: {
      type: Object,
      default: () => {},
      required: true
    }
  },
  created () {
    // 数据初始化
    this.initData()
  },
  methods: {
    onchange (checkedList) {
      const params = {}
      params.userId = this.userInfo.userId
      params.systemPrivileges = checkedList
      setTimeout(() => {
        this.$http.post(saveUserSystemPermission,params).then(() => {
        }).catch(() => {
          this.initData()
        })
      }, 50)
    },
    initData () {
      if (this.userInfo) {
        const params = {}
        params.userId = this.userInfo.userId
        setTimeout(() => {
          this.$http.post(getUserSystemPermission,params).then((res) => {
            if (res.result.hasPermission && res.result.hasPermission.length > 0) {
              this.checkedList = res.result.hasPermission
            }
            const userPermission = res.result.userPermission
            if (userPermission && userPermission.length > 0) {
              for (var i = 0; i < userPermission.length; i++) {
                if (userPermission[i] === 'SUPERADMIN') {
                  this.sceneDisabled = false
                  this.functionDisabled = false
                  this.orgDisabled = false
                  this.flowNodeDisabled = false
                  break
                }
                if (userPermission[i] === 'ALLSCENE') {
                  this.sceneDisabled = false
                }
                if (userPermission[i] === 'ALLFUNCTION') {
                  this.functionDisabled = false
                }
                if (userPermission[i] === 'ALLORG') {
                  this.orgDisabled = false
                }
                if (userPermission[i] === 'ALLFLOWNODE') {
                  this.flowNodeDisabled = false
                }
              }
            }
          }).catch(() => {
          })
        }, 50)
      } else {
        this.checkedList = []
      }
    }
  }
}
</script>
